package cn.m3a1.sys;

import cn.m3a1.model.Course;
import cn.m3a1.model.Studentofclass;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.DBConnection;
import com.iceking.dynamicweb.DWPConsole;
import com.iceking.dynamicweb.SysInitorIntf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;


/**
 * @author wyj
 * @date 2019/5/23
 * @time 下午7:52
 */
public class ProjectStarter implements SysInitorIntf {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private void initDataBase() {
        try {

            DBConnection connection= DWPConsole.getInstance().getOneConnection();
            long total=connection.total(User.class, null);
            System.out.println("查询到"+total);
            if(total<204) {
                logger.info("放入200条模拟数据");
                connection.startTransaction();
                for(int i=1;i<=4;i++) {
                    User user=new User();
                    user.setName("teacher"+i);
                    user.setEmail("teacher"+i+"@qq.com");
                    user.setCode(""+888886+i);
                    user.setUsername("teacher"+i);
                    user.setPassword("teacher"+i);
                    user.setTel(""+(int)((Math.random()*9+1)*100000));
                    user.setType(50);
                    connection.save(user);
                }

                connection.commit();
                connection.stopTransaction();
            }
             total=connection.total(Studentofclass.class, null);
            if (total==0){
                connection.startTransaction();
                for (int i=0;i<4;i++){
                    for (int j=1;j<=50;j++){
                        Studentofclass studentofclass=new Studentofclass();
                        studentofclass.setClassid(i+1);
                        studentofclass.setStudentid(i*50+j);
                        connection.save(studentofclass);
                    }
                }
                connection.commit();
                connection.stopTransaction();
            }
//            total=connection.total(Course.class, null);
//            if (total==0){
//                for (int i=1;i<=4;i++){
//                    Course course=new Course();
//
//                }
//            }
            connection.close();
        } catch (Exception e) {
            logger.error("数据库初始化错误"+e.getMessage(),e);
        }
    }
    @Override
    public void init() {
        //initDataBase();
    }

    @Override
    public void destroy() {

    }
}
